package com.hex.ds.hdrs.period.job.service;

import com.hex.ds.hdrs.period.job.dao.JobTaskLocationDao;
import com.hex.ds.hdrs.period.job.po.JobTaskLocation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lwz
 * @date 2021-01-07
 * @des
 */

@Slf4j
@Service
public class JobTaskLocationService {

    @Autowired
    private JobTaskLocationDao jobTaskLocationDao;

    /**
     * @Method: add
     * @Param: [jobTaskLocation]
     * @Return: void
     * @Description: 添加作业依赖节点坐标信息
     * @Author: ji.tang
     * @Date: 2022/11/22 14:44
     * @Version: V1.0
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void add(JobTaskLocation jobTaskLocation){
        this.jobTaskLocationDao.add(jobTaskLocation);
    }

    /**
     * @Method: delByJobCode
     * @Param: [jobCode]
     * @Return: void
     * @Description: 删除某个作业所有依赖节点坐标
     * @Author: ji.tang
     * @Date: 2022/11/22 14:44
     * @Version: V1.0
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void delByJobCode(String jobCode){
        this.jobTaskLocationDao.delByJobCode(jobCode);
    }

    public void saveAndUpdate(List<JobTaskLocation> list){
        if (CollectionUtils.isNotEmpty(list)){
            String jobCode = list.get(0).getJobCode();
            this.delByJobCode(jobCode);
            for (JobTaskLocation location : list){
                this.add(location);
            }
        }
    }
}
